from pyglet.window import Window as PygWindow
from pyglet import gl

class Window(PygWindow):
    def __init__(self, 
        virtual_width=None,
        virtual_height=None,
        keep_aspect=True,
        **kwargs):
        PygWindow.__init__(self, **kwargs)
        self._virtual_width = virtual_width or self.width
        self._virtual_height = virtual_height or self.height
        if keep_aspect:
            self._virtual_aspect = self._virtual_width / float(self._virtual_height)

    def _get_virtual_width(self):
        return self._virtual_width
    def _set_virtual_width(self, v):
        self._virtual_width = v
        self._recalc_virtual_transform()
    virtual_width = property(_get_virtual_width, _set_virtual_width)

    def _get_virtual_height(self):
        return self._virtual_height
    def _set_virtual_height(self, v):
        self._virtual_height = v
        self._recalc_virtual_transform()
    virtual_height = property(_get_virtual_height, _set_virtual_height)

    def _get_virtual_aspect(self):
        return self._virtual_aspect
    def _set_virtual_aspect(self, v):
        assert(type(v) is float)
        self._virtual_aspect = v
        self._recalc_virtual_transform()
    virtual_aspect = property(_get_virtual_aspect, _set_virtual_aspect)

    def _get_virtual_size(self):
        return self._virtual_width, self._virtual_height
    def _set_virtual_size(self, v):
        self._virtual_width = v[0]
        self._virtual_height = v[1]
        self._recalc_virtual_transform()
    virtual_size = property(_get_virtual_size, _set_virtual_size)

    def _recalc_virtual_transform(self):
        self._virtual_scale = (self._virtual_width / self.width,
                               self._virtual_height / self.height)

    def on_resize(self, width, height):
        PygWindow.on_resize(self, width, height)
        self._recalc_virtual_transform()

    def transform(self):
        gl.glLoadIdentity()
        gl.glScale2f(*self._virtual_scale)



